<template>
  <div v-loading="loading" element-loading-text="加载中。。。">
    <el-card style="margin-bottom:10px">
      <el-form ref='formInline' :model='formInline' size='mini' label-position="top">
        <el-row :gutter="10">
          <el-col :lg="4" :md="6">
            <el-form-item label='机构组编号' prop='BranchGroupNum'>
              <el-input v-model='formInline.BranchGroupNum' placeholder="请输入机构组编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label='机构组名称' prop='BranchGroupName'>
              <el-input v-model='formInline.BranchGroupName' placeholder="请输入机构组名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop">
            <el-button type='primary' size='mini' icon='el-icon-search' @click="branchGroupQuery">查询</el-button>
          </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop">
            <el-button type='warning' size='mini' icon='el-icon-refresh' @click="resetForm('formInline')">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card style="margin-top:10px;">
      <el-row>
        <el-col :lg="2" :md="3">
          <el-button type='primary' size='mini' icon='el-icon-plus' v-if="btnPermission('Button_DevBrand_Add')" @click='tradingGroupAdd()'>新增</el-button>
        </el-col>
        <el-col :lg="2" :md="3">
          <el-button type='primary' size='mini' icon='el-icon-edit' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Edit')" @click='tradingGroupUpdate()'>修改</el-button>
        </el-col>
        <el-col :lg='2' :md='3'>
          <el-button type='danger' size='mini' icon='el-icon-delete' class='b-button' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Delete')" @click='delDialogShow'>删除</el-button>
        </el-col>
        <el-col :lg='2' :md='3'>
          <el-button type='primary' size='mini' class='b-button' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Delete')" @click='assignBranch'>分配机构</el-button>
        </el-col>
        <el-col :lg='2' :md='3'>
          <el-button type='primary' size='mini' class='b-button' :disabled="!checkedUser" v-if="btnPermission('Button_DevBrand_Delete')" @click='assignTran'>分配交易组</el-button>
        </el-col>
      </el-row>
      <!-- 列表 -->
      <el-table ref="dataTypeInfo" :data='branchGroupInfoList' border  max-height="400" class="tableMarginTop" header-row-class-name="tableHeaderClass" highlight-current-row @selection-change="handleCurrentChangeTable">
        <el-table-column type="selection" width="40px"></el-table-column>
        <el-table-column prop='branchgroupnum' label='机构组编号'></el-table-column>
        <el-table-column prop='branchgroupname' label='机构组名称'></el-table-column>
        <el-table-column prop='legalpersonnum' label='法人号'></el-table-column>
      </el-table>
      <div style="text-align:right;margin-top:20px">
        <el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="10" layout="total, prev, pager, next, jumper" :total="pageSize">
        </el-pagination>
      </div>
    </el-card>
    <!-- 弹窗 -->
    <keep-alive>
      <component :is='dialogList.createTradingGroup' :createBrand='dialogOptions' @refreshBrand="branchGroupQuery"></component>
    </keep-alive>
    <keep-alive>
      <component :is='dialogList.updateTradingGroup' :option='UpdateDialogOptions' @refreshBrand="branchGroupQuery"></component>
    </keep-alive>
    <keep-alive>
      <component :is='dialogList.assignBranch' :option='assignBranchDialogOptions' @refreshBrand="branchGroupQuery"></component>
    </keep-alive>
    <keep-alive>
      <component :is='dialogList.assignTran' :option='assignTranDialogOptions' @refreshBrand="branchGroupQuery"></component>
    </keep-alive>
  </div>
</template>

<script>
import request from "@/common/utils/request";
import getGlobalParams from "@/common/utils/getGlobalParams";
import createTradingGroup from "./dialog/createTradingGroup";
import updateTradingGroup from "./dialog/updateTradingGroup";
import assignBranch from "./dialog/assignBranch";
import assignTran from "./dialog/assignTran";
import branchGroup from "@/message/branchManage/branchGroupMgr/branch-group-mgr";

export default {
  data() {
    return {
      dialogList: { createTradingGroup, updateTradingGroup, assignBranch, assignTran },
      dialogOptions: { isShow: false },
      UpdateDialogOptions: { isShow: false },
      assignBranchDialogOptions: { isShow: false },
      assignTranDialogOptions: { isShow: false },
      formInline: {
        BranchGroupName:"",
        BranchGroupNum:""
      },
      loading: false,
      currentPage: 1,
      pageSize: undefined,
      checkedUser: false,
      currentRow: [],
      branchGroupInfoList: []
    };
  },
  created() {
    this.branchGroupQuery();
  },
  methods: {
    /**
     * 删除交易组方法
     */
    delBranchGroup() {
      let resBody = new branchGroup();
      this.loading = true;
      resBody.data.BranchGroupAppNum = this.currentRow.appnum;
      resBody.data.BranchGroupNum = this.currentRow.branchgroupnum;
      resBody.data.BranchGroupName = this.currentRow.branchgroupname;
      resBody.data.OperType = "D"; // 操作类型
      request(resBody)
        .then(response => {
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.loading = false;
            this.$message({
              type: "success",
              message: "删除成功"
            });
            this.branchGroupQuery();
          } else {
            this.loading = false;
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          this.loading = false;
          this.$message({
            type: "info",
            message: error
          });
        });
    },

    // 删除事件
    delDialogShow() {
      this.$confirm("此操作将永久删除该交易组, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        closeOnClickModal: false
      })
        .then(() => {
          this.delBranchGroup();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 表格选中后值改变
    handleCurrentChangeTable(val) {
      //保存当前选中数据并判断按钮是否可用
      this.currentRow = val[0];
      this.UpdateDialogOptions.currentRow = this.currentRow;
      this.assignBranchDialogOptions.currentRow = this.currentRow;
      this.assignTranDialogOptions.currentRow = this.currentRow;
      if (val.length == 1) {
        this.checkedUser = true;
      } else if (val.length > 1) {
        this.checkedUser = false;
      } else {
        this.checkedUser = false;
      }
    },

    //打开添加弹窗
    tradingGroupAdd() {
      this.dialogOptions.isShow = true;
    },

    //打开修改弹窗
    tradingGroupUpdate() {
      this.UpdateDialogOptions.isShow = true;
    },

    //打开分配机构弹窗
    assignBranch() {
      this.assignBranchDialogOptions.isShow = true;
    },

    //打开分配交易组弹窗
    assignTran() {
      this.assignTranDialogOptions.isShow = true;
    },

    //清除表单数据
    resetForm(item) {
      this.$refs[item].resetFields();
    },

    // 切换分页查询表数据
    handleCurrentChange(val) {
      this.currentPage = val;
      this.branchGroupQuery();
    },

    /**
     * 查询表格数据
     */
    branchGroupQuery() {
      this.loading = true;
      let resBody = new branchGroup();
      resBody.data.BranchGroupNum = this.formInline.BranchGroupNum;
      resBody.data.BranchGroupName = this.formInline.BranchGroupName;
      resBody.data.LegalPersonNum = this.$store.getters.LegalPersonNum;
      resBody.data.OperType = "Q"; // 操作类型
      // 查询公共参数
      resBody.data.length = getGlobalParams.get("PageSize");
      resBody.data.draw = this.currentPage;
      resBody.data.start =
        (this.currentPage - 1) * getGlobalParams.get("PageSize");
      request(resBody)
        .then(response => {
          this.loading = false;
          if (response.SYS_HEAD.ReturnCode === "000000") {
            this.branchGroupInfoList = response.RspInfo.pageList || [];
            this.pageSize = response.RspInfo.recordsTotal;
          } else {
            this.branchGroupInfoList = [];
            this.pageSize = 0;
            this.$message({
              type: "error",
              message: response.SYS_HEAD.ReturnMessage
            });
          }
        })
        .catch(error => {
          this.loading = false;
          //   this.logger.log("fail", error);
        });
    }
  }
};
</script>


<style scoped lang="scss">
</style>
